<!DOCTYPE html>
<html lang="ch">
<head>
    <meta charset="UTF-8">
    <title>新增商品</title>
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/addProduct.css">
    <link rel="stylesheet" href="css/registerCommon.css">
</head>
<body>
<div class="nav">新增商品</div>
<div class="container">
    <div class="dialog">
        <h2>新增商品</h2>

        <div class="row">
            <span>商品名</span>
            <input type="text" id="productName" placeholder="请输入商品名称">
        </div>

        <div class="row">
            <span>品牌</span>
            <input type="text" id="brand" placeholder="请输入品牌">
        </div>
        <div class="row">
            <span>型号</span>
            <input type="text" id="model" placeholder="请输入型号">
        </div>
        <div class="row">
            <span>描述</span>
            <input name="description" id="description" placeholder="请输入商品描述" type="text" >
        </div>
        <div class="row">
            <span>价格</span>
            <input name="price" id="price" placeholder="请输入价格" type="number" >
        </div>
        <div class="row">
            <span>库存</span>
            <input name="stock" id="stock" placeholder="请输入库存" type="number" >
        </div>
        <div class="row">
            <span>种类</span>
            <input name="category" id="category" placeholder="请输入所属类别" type="text" >
        </div>
        <div class="row">
            <span>验证码</span>
            <input name="verifyCode" id="verifyCode" placeholder="请输入验证码" type="text" >
        </div>
        <div class="row">
            <img title="单击图片刷新！" id="imgCode" src="/util/verifycode" onclick="this.src='/util/verifycode'"/>
        </div>
        <div class="row">
            <button class="submit" onclick="addProduct()">提交</button>
        </div>

    </div>

</div>
<script src="js/jquery.min.js"></script>
<script>
    let queryString = location.search;
    // 创建 URLSearchParams 对象
    let urlParams = new URLSearchParams(queryString);
    // 获取特定参数的值，假设要获取名为paramName的参数
    let storeId = urlParams.get("storeId");
    function addProduct() {
        //校验参数
        let pass = true;
        $(' input').each(function () {
            let inputValue = $(this).val();
            if (inputValue === null || inputValue === "") {
                const labelText = $(this).prev('span').text();
                pass = false;
                alert("请输入" + labelText);
                return false;
            }
        });
        if (!pass) {
            return;
        }
        $.ajax({
            type: 'post',
            url: '/product/addProduct',
            data: {
                storeId: storeId,
                productName: $("#productName").val(),
                brand: $("#brand").val(),
                model: $("#model").val(),
                description: $("#description").val(),
                price: $("#price").val(),
                stock: $("#stock").val(),
                category: $("#category").val(),
                verifyCode: $("#verifyCode").val()
            }
            ,success: function(result) {
                if(result.code === "SUCCESS") {
                    alert("添加成功");
                    history.back();
                    // location.replace("/myProduct.html" + location.search);
                }else{
                    alert(result.errMsg);
                    $("#imgCode").click();
                }
            }
            ,error: function(error) {
                if (error != null && error.status === 401) {
                    alert("请先登录");
                    location.replace("/login.html");
                } else {
                    alert("内部错误");
                }
            }
        });
    }
</script>
</body>
</html>